package cn.zmit.kuxi.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import cn.zmit.kuxi.R;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.zmit.kuxi.fragment.JoinRecordFragmentAll;
import cn.zmit.kuxi.fragment.JoinRecordFragmentAlready;
import cn.zmit.kuxi.fragment.JoinRecordFragmentDoing;

/** * @author @parameter * @return */
public class JoinRecordActivity extends BaseActivity {
	@ViewInject(R.id.join_record_radio_button_group)
	private RadioGroup join_record_radio_button_group;
	@ViewInject(R.id.radbtn_all_0)
	private RadioButton radbtn_all_0;
	@ViewInject(R.id.radbtn_doing_1)
	private RadioButton radbtn_doing_1;
	@ViewInject(R.id.radbtn_already_2)
	private RadioButton radbtn_already_2;
	@ViewInject(R.id.view_indicator_phone)
	private View view_1;
	@ViewInject(R.id.view_indicator_computer)
	private View view_2;
	@ViewInject(R.id.view_indicator_cammer)
	private View view_3;

	private FragmentManager fragmentManager;
	private JoinRecordFragmentAll joinRecordFragmentAll;
	private JoinRecordFragmentDoing joinRecordFragmentDoing;
	private JoinRecordFragmentAlready joinRecordFragmentAlready;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("参与记录", true);
		setContentView(R.layout.activity_join_record);
		ViewUtils.inject(this);
		fragmentManager = getFragmentManager();
		setTabSelection(0);
	}

	private void toggleIndicator(int id) {
		switch (id) {
		case R.id.radbtn_all_0:
			view_1.setBackgroundColor(getResources().getColor(R.color.red2));
			view_2.setBackgroundColor(getResources().getColor(R.color.gray));
			view_3.setBackgroundColor(getResources().getColor(R.color.gray));
			break;
		case R.id.radbtn_doing_1:
			view_1.setBackgroundColor(getResources().getColor(R.color.gray));
			view_2.setBackgroundColor(getResources().getColor(R.color.red2));
			view_3.setBackgroundColor(getResources().getColor(R.color.gray));
			break;

		case R.id.radbtn_already_2:
			view_1.setBackgroundColor(getResources().getColor(R.color.gray));
			view_2.setBackgroundColor(getResources().getColor(R.color.gray));
			view_3.setBackgroundColor(getResources().getColor(R.color.red2));
			break;

		default:
			break;
		}
	}

	private void hideFragments(FragmentTransaction transaction) {
		if (joinRecordFragmentAll != null) {
			transaction.hide(joinRecordFragmentAll);
		}
		if (joinRecordFragmentDoing != null) {
			transaction.hide(joinRecordFragmentDoing);
		}
		if (joinRecordFragmentAlready != null) {
			transaction.hide(joinRecordFragmentAlready);
		}
	}
	private void clear(){
		radbtn_all_0.setTextColor(getResources().getColor(R.color.black));
		radbtn_doing_1.setTextColor(getResources().getColor(R.color.black));
		radbtn_already_2.setTextColor(getResources().getColor(R.color.black));
	}
	private void setTabSelection(int index) {
		clear();
		// 开启事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// 隐藏所有fragment
		hideFragments(transaction);
		switch (index) {
		case 0:
			radbtn_all_0.setTextColor(getResources().getColor(R.color.red2));
			if (joinRecordFragmentAll==null) {
				joinRecordFragmentAll=new JoinRecordFragmentAll();
				transaction.add(R.id.join_record_content, joinRecordFragmentAll);
				
			}else {
				transaction.show(joinRecordFragmentAll);
			}
			break;
		case 1:
			radbtn_doing_1.setTextColor(getResources().getColor(R.color.red2));
			if (joinRecordFragmentDoing==null) {
				joinRecordFragmentDoing=new JoinRecordFragmentDoing();
				transaction.add(R.id.join_record_content, joinRecordFragmentDoing);
				
			}else {
				transaction.show(joinRecordFragmentDoing);
			}
			break;
		case 2:
			radbtn_already_2.setTextColor(getResources().getColor(R.color.red2));
			if (joinRecordFragmentAlready==null) {
				joinRecordFragmentAlready=new JoinRecordFragmentAlready();
				transaction.add(R.id.join_record_content, joinRecordFragmentAlready);
			}else {
				transaction.show(joinRecordFragmentAlready);
			}
			break;
		default:
			break;
		}
		transaction.commit();
	}
	@OnClick({R.id.radbtn_all_0,R.id.radbtn_doing_1,R.id.radbtn_already_2})
	private void OnClick(View v){
		switch (v.getId()) {
		case R.id.radbtn_all_0:
			toggleIndicator(R.id.radbtn_all_0);
			setTabSelection(0);
			break;
		case R.id.radbtn_doing_1:
			toggleIndicator(R.id.radbtn_doing_1);
			setTabSelection(1);
			break;
		case R.id.radbtn_already_2:
			toggleIndicator(R.id.radbtn_already_2);
			setTabSelection(2);
			break;
		default:
			break;
		}
	}
}
